var playerStateDao = require('../dao/common/playerStateDao');
var playerState = require('../domain/common/playerState');
var utils = require('../util/utils');
var playerStateService = module.exports;

var action = {
    GO_ON: 'GO_ON',
    RECOVER: 'RECOVER'
};

playerStateService.inPreparedRoom = function (name, extra, cb) {
    playerStateDao.updatePlayerState(name, playerState.STATE.IN_PREPARED_ROOM, extra, cb);
};

playerStateService.outPreparedRoom = function (name, extra, cb) {
    playerStateDao.updatePlayerState(name, playerState.STATE.AFTER_LOGIN, extra, cb);
};

playerStateService.afterLogin = function (name, extra, cb) {
    playerStateDao.getPlayerState(name, function (err, myState) {
        if (myState && myState.state) {
            if (myState.state === playerState.STATE.AFTER_LOGIN) {
                utils.invokeCallback(cb, null, action.GO_ON);
            } else {
                utils.invokeCallback(cb, null, action.RECOVER);
            }
        } else {
            // first login
            playerStateDao.createPlayerState(name, extra, function (err, playerState) {
                utils.invokeCallback(cb, null, action.GO_ON);
            });
        }
    });
};


playerStateService.inKillerGame = function (name, extra, cb) {
    playerStateDao.updatePlayerState(name, playerState.STATE.IN_KILLER_GAME, extra, cb);
};

playerStateService.outKillerGame = function (name, extra, cb) {
    playerStateDao.updatePlayerState(name, playerState.STATE.AFTER_LOGIN, extra, cb);
};

playerStateService.beforeKillerGame = function (name, extra, cb) {
    playerStateDao.updatePlayerState(name, playerState.STATE.BEFORE_KILLER_GAME, extra, cb);
};

playerStateService.getPlayerState = function (name, cb) {
    playerStateDao.getPlayerState(name, cb);
};



